﻿using System.Collections;
using BaseData.CommonLib.Models;
using BaseData.RazorLib.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Radzen;

namespace BaseData.RazorLib.Pages;

public partial class MaterialCategoryEdit
{
    private MaterialCategoryEditRequestDto _editRequestDto = new();
    private List<MaterialCategoryTreeResponseDto> TreeData = new();

    [Inject] public IMaterialCategoryService MaterialCategoryService { get; set; }

    [Inject] public DialogService DialogService { get; set; }

    [Parameter] public int Key { get; set; }

    protected override async Task OnParametersSetAsync()
    {
        _editRequestDto = new MaterialCategoryEditRequestDto();
        //_editRequestDto.ParentName = ParentName;
        //_editRequestDto.ParentId = ParentId;
        if (Key > 0)
        {
            var result = await MaterialCategoryService.GetMaterialCategory(Key);
            if (result != null && result.Succeeded && result.Data != null)
            {
                _editRequestDto.Code = result.Data.Code;
                _editRequestDto.Name = result.Data.Name;
                _editRequestDto.ParentId = result.Data.ParentId;
                _editRequestDto.Description = result.Data.Description;
            }
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();


        var categoryTreeResult = await MaterialCategoryService.QueryMaterialCategoriesByTree();
        if (categoryTreeResult != null && categoryTreeResult.Succeeded)
        {
            TreeData = categoryTreeResult.Data!;
        }
    }

    async Task OnSubmit()
    {
        if (Key != null)
        {
            var result = await MaterialCategoryService.UpdateMaterialCategory(Key, _editRequestDto);

            if (result.Succeeded)
            {
                DialogService.Close(null);
            }
        }
        else
        {
            var result = await MaterialCategoryService.CreateMaterialCategory(_editRequestDto);

            if (result.Succeeded)
            {
                DialogService.Close(null);
            }
        }
    }


    protected void CancelButtonClick(MouseEventArgs args)
    {
        DialogService.Close(null);
    }

    object GetItemFromTreeHandler(IEnumerable data, object value)
    {
        MaterialCategoryTreeResponseDto? materialCategoryTreeResponseDto = null;
        var materialCategoryTreeResponseDtos = (IList<MaterialCategoryTreeResponseDto>)data;
        if (value != null!)
        {
            var currentValue = (int)value;
            if (materialCategoryTreeResponseDtos.Count > 0 && currentValue > 0)
            {
                materialCategoryTreeResponseDto = GetChildSelectedItem(materialCategoryTreeResponseDtos, currentValue);
            }
        }

        return materialCategoryTreeResponseDto!;
    }

    private MaterialCategoryTreeResponseDto? GetChildSelectedItem(
        IList<MaterialCategoryTreeResponseDto> treeResponseDtos, int value)
    {
        MaterialCategoryTreeResponseDto? item = null;
        foreach (var childItem in treeResponseDtos)
        {
            if (childItem.Children != null)
            {
                return GetChildSelectedItem(childItem.Children, value);
            }

            if (childItem.Key == value)
            {
                item = childItem;
                break;
            }
        }

        return item;
    }
}